<?php

namespace Unl\HomologacionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Unl\HomologacionBundle\Entity\PeticionHomologacion;
use Unl\HomologacionBundle\Form\PeticionHomologacionType;
use Symfony\Component\HttpFoundation\Response;

/**
 * 
 *
 * 
 */
class BaseController extends Controller {

    /**
     * @Route("/listByUniversidad", name="_carrerasByUniversidadId")
     */
    public function getByUniversidadId() {
        $em = $this->getDoctrine()->getEntityManager();
        $universidadId = $this->get('request')->query->get('data');
        $carreras = $em->getRepository('UnlHomologacionBundle:Carrera')->findByUniversidadId($universidadId);
        $html = '';
        foreach ($carreras as $carrera) {
            $html = $html . sprintf("<option value=\"%d\">%s</option>", $carrera->getId(), $carrera->getNombre());
        }

        return new Response($html);
    }

    /**
     * @Route("/listByMallaCurricular", name="_unidadesByMallaCurricularId")
     */
    public function getByMallaCurricularId() {

        $mallaCurricularId = $this->get('request')->query->get('data');

        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery('SELECT u FROM UnlHomologacionBundle:Unidad u ' .
                ' JOIN u.modulo m ' .
                ' JOIN m.mallaCurricular mc ' .
                'WHERE mc.id = :id ' .
                'ORDER BY u.id ASC');
        $query->setParameter('id', $mallaCurricularId);
        $unidades = $query->getResult();

        if (!$unidades == null) {
            foreach ($unidades as $unidad) {
                $unidadesArray[] = array(
                    "modulo" => $unidad->getModulo()->getNumeroModulo(),
                    "nombre" => $unidad->getNombre(),
                    "numeroHoras" => $unidad->getNumeroHoras(),
                    "numeroCreditos" => $unidad->getNumeroCreditos()
                );
            }
            $json = json_encode($unidadesArray);
            return new Response($json);
        } else {
            return new Response();
        }
        
    }
    
    
    /**
     * @Route("/listByMallaCurricular", name="_unidadesPorMallaCurricularId")
     */ 
    public function getPorMallaCurricularId($malla) {

        $mallaCurricularId = $malla;

        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery('SELECT u FROM UnlHomologacionBundle:Unidad u ' .
                ' JOIN u.modulo m ' .
                ' JOIN m.mallaCurricular mc ' .
                'WHERE mc.id = :id ' .
                'ORDER BY u.id ASC');
        $query->setParameter('id', $mallaCurricularId);
        $unidades = $query->getResult();

        return $unidades;
        
        
    }
    
    /**
     * @Route("/ayuda", name="_ayuda")
     */
    public function ayudaAction($url) {
        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery('SELECT a FROM UnlHomologacionBundle:Ayuda a ' .
                'WHERE a.nombre = :nombre ');
        $query->setParameter('nombre', $url);
        $ayudas = $query->getResult();
        
        $descripcion = 'Ayuda no definida!!';
        if($ayudas !=  null){
            $ayuda = reset($ayudas);
            $descripcion = $ayuda->getDescripcion();
            
            $descripcion = '<h2>'.$ayuda->getTitulo().'</h2>'.$descripcion;
        }
                
        return  new Response($descripcion);
    }
    
    
    /**
     * @Route("/listPorMallaCurricular", name="_mallasPorCarreraId")
     */
    public function getByCarreraId() {

        $em = $this->getDoctrine()->getEntityManager();
        $carreraId = $this->get('request')->query->get('data');
        $mallas = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->findByCarreraId($carreraId);
        $html = sprintf("<option value=\"%d\">%s</option>", 0, "Elija la malla curricular");
        foreach ($mallas as $malla) {
            
            $html = $html . sprintf("<option value=\"%d\"> %d %s</option>", $malla->getId(), $malla->getAnio(), $malla->getCarrera());
        }

        return new Response($html);
    }
    
    
    /**
     * @Route("/ByOtrasUniversidades", name="_universidadById")
     */
    public function getOtrasUniversidades() {
        $em = $this->getDoctrine()->getEntityManager();
        $universidadId = $this->get('request')->query->get('data');
        $universidades = $em->getRepository('UnlHomologacionBundle:Universidad')->findById($universidadId);
        $html = '';
        foreach ($universidades as $universidad) {
            $html = $html . sprintf("<option value=\"%d\">%s</option>", $universidad->getId(), $universidad->getNombre());
        }

        return new Response($html);
    }
    
    /**
     * @Route("/listByPersona", name="_personasByDocente")
     */
    public function getByPersonaDocente(){
        $em = $this->getDoctrine()->getEntityManager();
        $personaDocente = 'a:1:{i:0;s:12:"ROLE_DOCENTE";}';
        $personas = $em->getRepository('UnlHomologacionBundle:Persona')->findByPersonaDocente($personaDocente);
        $html = '';
        foreach ($personas as $persona) {
            $html = $html . sprintf("<option value=\"%d\">%s %s</option>", $persona->getId(), $persona->getNombre(),$persona->getApellido());
        }

        return new Response($html);
        
    }

    
    protected function enviarCorreo($email, $asunto, $cuerpo) {

      
        $message = \Swift_Message::newInstance()     // we create a new instance of the Swift_Message class
                ->setSubject($asunto)     // we configure the title
                ->setFrom('shomologa@gmail.com')     // we configure the sender
                ->setTo($email)     // we configure the recipient
               // ->setCc($emailDocente)
                //->setBody($this->renderView('UnlHomologacionBundle:PeticionHomologacion:email.txt.twig', array('name' => $name)))
                ->setBody($cuerpo)
        // and we pass the $name variable to the text template which serves as a body of the message

        ;

        $this->get('mailer')->send($message);     // then we send the message.
        return false;
    }
    
}
